﻿using System.Collections.Generic;

namespace Mozi.Live.SDP
{
    /// <summary>
    /// 会话级描述信息
    /// </summary>
    public class SDPSessionDescription
    {
        /// <summary>
        /// 版本号
        /// </summary>
        public int Version { get; set; }
        /// <summary>
        /// 视频源
        /// </summary>
        public SDPOrigin Origin { get; set; }
        /// <summary>
        /// 会话名称
        /// </summary>
        public string SessionName { get; set; }
        /// <summary>
        /// 会话信息
        /// </summary>
        public string SessionInformation { get; set; }
        /// <summary>
        /// 会话描述地址
        /// </summary>
        public string Uri { get; set; }
        /// <summary>
        /// 会话责方联系邮箱 
        /// 范式：
        /// <list type="table">
        ///     <item>Jason&#060;brotherqian&#064;163.com&#062;</item>
        ///     <item>brotherqian@163.com(Jason)</item>
        /// </list>
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// 会话责方联系电话
        /// </summary>
        public string Phone { get; set; }
        /// <summary>
        /// 链接地址信息  <![CDATA[<base multicast address>[/<ttl>]/<number of addresses>]]>
        /// </summary>
        public SDPConnection Connection { get; set; }
        /// <summary>
        /// 频带信息
        /// </summary>
        public List<SDPBandWidth> BandWidths { get; set; }
        /// <summary>
        /// 已过时，不再使用
        /// </summary>
        public string EncryptKeys { get; set; }
        /// <summary>
        /// 时间描述信息
        /// </summary>
        public List<SDPTimeDescription> TimeDescriptions { get; set; }
        //TODO 此处还有问题
        /// <summary>
        /// 时区适配信息，该字段与RepeatTime紧密结合
        /// </summary>
        public string TimezoneAdjust { get; set; }
        /// <summary>
        /// 附加属性
        /// </summary>
        public List<SDPAttribute> Attributes { get; set; }
        /// <summary>
        /// 实例化
        /// </summary>
        public SDPSessionDescription()
        {
            Version = 1;
        }
        /// <summary>
        /// 输出SDP格式文档
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            List<string> sb = new List<string>
            {
                $"v={Version}",
                $"o={Origin}",
                $"s={SessionName??"-"}",
                $"i={SessionInformation}",
                $"u={Uri}"
            };

            if (string.IsNullOrEmpty(Email))
            {
                sb.Add($"e={Email}");
            }

            if (string.IsNullOrEmpty(Phone))
            {
                sb.Add($"p={Phone}");
            }

            if (Connection != null)
            {
                sb.Add($"c={Connection}");
            }

            if (BandWidths != null)
            {
                foreach (var BandWidth in BandWidths)
                {
                    sb.Add($"b={BandWidth}");
                }
            }

            if (TimeDescriptions != null)
            {
                foreach (var tp in TimeDescriptions)
                {
                    sb.Add(tp.ToString());
                }
            }
            if (TimezoneAdjust != null)
            {
                sb.Add($"z={TimezoneAdjust}");
            }
            if (Attributes != null)
            {
                foreach (var att in Attributes)
                {
                    sb.Add($"a={att}");
                }
            }

            return string.Join("\r\n", sb);
        }

        /// <summary>
        /// 查找同类型的属性
        /// </summary>
        /// <param name="attribute"></param>
        /// <returns></returns>
        public SDPAttribute[] GetAttributes(SDPAttributeType attribute)
        {
            if (Attributes != null)
            {
                return Attributes.FindAll(x => x.Attribute == attribute).ToArray();
            }
            else
            {
                return null;
            }
        }
    }

}
